function showDiv(elementId) {
	var mask = document.getElementById('mask');
	mask.style.display = "";
	var element = document.getElementById(elementId);
	element.style.display = "";
}
function hiddenDiv(elementId) {
	var mask = document.getElementById('mask');
	mask.style.display = "none";
	var element = document.getElementById(elementId);
	element.style.display = "none";
}
function sign(id) {
	var storehouseIds = document.getElementsByName('storehouseId');
	for ( var i = 0; i < storehouseIds.length; i++) {
		var storehouseId = storehouseIds.item(i);
		if (storehouseId.value == id) {
			storehouseId.checked = true;
			break;
		}
	}
}
function insertButton() {
	clearValue();
	showDiv('sus');
}
function updateButton() {
	var storehouseIds = document.getElementsByName('storehouseId');
	var id = 0;
	for ( var i = 0; i < storehouseIds.length; i++) {
		var storehouseId = storehouseIds.item(i);
		if (storehouseId.checked == true) {
			id = storehouseId.value;
			break;
		}
	}
	update(id);
}
function deleteButton() {
	deleteStorehouse();
}
function selectButton() {
	clearValue();
	showDiv('sus');
	document.getElementById("add").style.display = "none";
	document.getElementById("modify").style.display = "none";
	document.getElementById("find").style.display = "";
}
function update(id, trid) {
	document.getElementById("add").style.display = "none";
	document.getElementById("modify").style.display = "";
	document.getElementById("find").style.display = "none";
	if (id != 0) {
		showDiv('sus');
	} else {
		alert("请选择仓库");
	}
	var trElement = document.getElementById('' + trid);
	var str = '';
	var tds = trElement.getElementsByTagName('td');
	var storehouseId = tds.item(0).getAttribute('storehouseId');
	var employeeId = tds.item(0).getAttribute('employeeId');
	var storehouseName = tds.item(0).getAttribute('storehouseName');
	var storehouseTel = tds.item(0).getAttribute('storehouseTel');
	var storehouseAdd = tds.item(0).getAttribute('storehouseAdd');
	var storehouseMemo = tds.item(0).getAttribute('storehouseMemo');
	str = storehouseId + '.' + employeeId + '.' + storehouseName + '.'
			+ storehouseTel + '.' + storehouseAdd + '.' + storehouseMemo;
	var strArr = str.split('.');
	setValue(strArr);
}
function clearValue() {
	document.getElementById("add").style.display = "";
	document.getElementById("modify").style.display = "none";
	document.getElementById("find").style.display = "none";
	$("#storehouseId").val("");
	document.all.storehouseName.value = "";
	document.all.storehouseTel.value = "";
	document.all.storehouseAdd.value = "";
	document.all.storehouseMemo.value = "";
}
function setValue(strArr) {
	$("#storehouseId").val(strArr[0]);
	document.all.empId.value = strArr[1];
	document.all.storehouseName.value = strArr[2];
	document.all.storehouseTel.value = strArr[3];
	document.all.storehouseAdd.value = strArr[4];
	document.all.storehouseMemo.value = strArr[5];
}
function callback(data) {
	if ("" != data.lenght) {
		hiddenDiv('sus');
	}
	var str = "";
	for ( var i = 0; i < data.length; i++) {
		// 动态的改变页面中div节点中的内容
		str = str + '<tr id="tr' + i + '" ondblclick="update('
				+ data[i].storehouseId + ',\'tr' + i + '\');" onclick="sign('
				+ data[i].storehouseId + ');">'
				+ '<td width="20" storehouseId="' + data[i].storehouseId
				+ '" storehouseName="' + data[i].storehouseName
				+ '" employeeName="' + data[i].employeeName
				+ '" storehouseTel="' + data[i].storehouseTel
				+ '" storehouseAdd="' + data[i].storehouseAdd
				+ '" storehouseMemo="' + data[i].storehouseMemo
				+ '" employeeId="' + data[i].employeeId
				+ '"><input type="radio" name="storehouseId" value="'
				+ data[i].storehouseId + '" /></td>' + '<td width="100" >'
				+ data[i].storehouseName + '</td>' + '<td width="70" >'
				+ data[i].employeeName + '</td>' + '<td width="80" >'
				+ data[i].storehouseTel + '</td>' + '<td width="200" >'
				+ data[i].storehouseAdd + '</td>' + '<td width="92" >'
				+ data[i].storehouseMemo + '</td>' + '</tr>';
	}

	clearValue();
	$("#storehouseList").html(str);
}
// 添加 员工 部分
function createStorehouse() {
	var storehouseName = escape(escape($("#storehouseName").val()));
	var employeeId = escape(escape($("#empId").val()));
	var storehouseTel = escape(escape($("#storehouseTel").val()));
	var storehouseAdd = escape(escape($("#storehouseAdd").val()));
	var storehouseMemo = escape(escape($("#storehouseMemo").val()));
	var j = 0;
	var url = '/stock/pages/system/storehouse.do?method=createStorehouse&d='
			+ new Date() + '&storehouseName=' + storehouseName + '&employeeId='
			+ employeeId + '&storehouseTel=' + storehouseTel + '&storehouseAdd='
			+ storehouseAdd + '&storehouseMemo=' + storehouseMemo;
	$.getJSON(url, {}, callback);
}
// 修改 员工 部分
function updateStorehouse() {
	var storehouseId = $("#storehouseId").val();
	var storehouseName = escape(escape($("#storehouseName").val()));
	var employeeId = escape(escape($("#empId").val()));
	var storehouseTel = escape(escape($("#storehouseTel").val()));
	var storehouseAdd = escape(escape($("#storehouseAdd").val()));
	var storehouseMemo = escape(escape($("#storehouseMemo").val()));
	var url = '/stock/pages/system/storehouse.do?method=updateStorehouse&d='
			+ new Date() + '&storehouseId=' + storehouseId + '&storehouseName='
			+ storehouseName + '&employeeId=' + employeeId + '&storehouseTel='
			+ storehouseTel + '&storehouseAdd=' + storehouseAdd
			+ '&storehouseMemo=' + storehouseMemo;
	$.getJSON(url, {}, callback);
}
// 删除 员工 部分
function deleteStorehouse() {
	if (confirm("真的要删除吗？删除后将无法恢复！")) {
		var storehouseIdBox = document.getElementsByName("storehouseId");
		var storehouseId = 0;
		for ( var i = 0; i < storehouseIdBox.length; i++) {
			if (storehouseIdBox[i].checked) {
				isOper = storehouseIdBox[i].isOper;
				storehouseId = storehouseIdBox[i].value;
				break;
			}
		}
		var url = '/stock/pages/system/storehouse.do?method=deleteStorehouse&d='
				+ new Date()
				+ '&storehouseId='
				+ storehouseId
				+ '&isOper='
				+ isOper;
		$.getJSON(url, {}, callback);
	}
}
// 查找 员工 部分
function selectStorehouse() {
	var storehouseId = $("#storehouseId").val();
	var storehouseName = escape(escape($("#storehouseName").val()));
	var employeeId = escape(escape($("#empId").val()));
	var storehouseTel = escape(escape($("#storehouseTel").val()));
	var storehouseAdd = escape(escape($("#storehouseAdd").val()));
	var storehouseMemo = escape(escape($("#storehouseMemo").val()));
	var url = '/stock/pages/system/storehouse.do?method=selectStorehouse&d='
			+ new Date() + '&storehouseId=' + storehouseId + '&storehouseName='
			+ storehouseName + '&employeeId=' + employeeId + '&storehouseTel='
			+ storehouseTel + '&storehouseAdd=' + storehouseAdd
			+ '&storehouseMemo=' + storehouseMemo;
	$.getJSON(url, {}, callback);
}
function selectAllStorehouse() {
	var url = '/stock/pages/system/storehouse.do?method=selectStorehouse&d=' + new Date();
	$.getJSON(url, {}, callback);
}